#include "queue.h"
#include "common.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* 队列初始化 */
int QueueInit(Queue** queue)
{
    Queue* tmp = (Queue*)malloc(sizeof(Queue));
    if(tmp == NULL)
    {
        return MALLOC_ERROR;
    }
    memset(tmp, 0, sizeof(tmp));
    tmp->head = NULL;
    tmp->tail = tmp->head;
    tmp->size = 0;
    *queue = tmp;
    return ON_SUCCESS;
}
/* 入队 */
int QueuePush(Queue* queue, void* val)
{
    if(queue == NULL)
    {
        return NULL_PTR;
    }
    Node* tmp = (Node*)malloc(sizeof(Node));
    tmp->next = NULL;
    tmp->data = val;
    if(queue->size == 0)
    {
        queue->head = tmp;
    }
    else
    {
        queue->tail->next = tmp;
    }
    queue->tail = tmp;
    queue->size++;
    return ON_SUCCESS;
}
/* 队空 */
int QueueEmpty(Queue* queue)
{
    return (queue->size == 0) ? 1 : 0;
}
/* 出队 */
void* QueuePop(Queue* queue)
{
    Node* tmp = queue->head;
    queue->head = queue->head->next;
    queue->size--;
    void* val = tmp->data;
    if(tmp != NULL)
    {
        free(tmp);
        tmp == NULL;
    }
    return val;
}
/* 销毁队列 */
int QueueDestroy(Queue* queue)
{
    if(queue != NULL)
    {
        free(queue);
    }
    return ON_SUCCESS;
}